home *** CD-ROM | disk | FTP | other *** search
/ DOpus Plus / DOpus Plus.iso / Tutorial / C Guide / Simple_Module2 / Window.c < prev   
C/C++ Source or Header  |  1998-08-25  |  5KB  |  149 lines

  1. /*******************************************************************
  2.  
  3.    NotifyWindow.c
  4.     
  5.     Does open a simple window with some easy gadgets.
  6.     
  7.     You must (!) understand here everything. We does work always
  8.     with this basic code and add a little bit stuff.
  9.     
  10.     First function is always the function which is called from the
  11.     ModuleEntry.c. All others follow here like we does need them at
  12.     the first time.
  13.             
  14. *********************************************************************/
  15.  
  16. #include "includes/Window.h"
  17.  
  18. /********************************************************************/
  19. // locale prototypes
  20.  
  21. BOOL OpenDOpusWin( WindowHandle *wh );
  22.  
  23. BOOL HandleWindow( WindowHandle *wh );
  24.  
  25. /********************************************************************/
  26.  
  27. void OwnWindow( STRPTR args, struct Screen *screen )
  28. {
  29.     WindowHandle          *wh;
  30.     
  31.     if( (wh = AllocMemH(mempool, sizeof(WindowHandle))) ) // allocate some memory
  32.       {
  33.           wh->screen = screen;     // store the screen pointer
  34.           
  35.           if( OpenDOpusWin(wh) )   // open the window
  36.            {
  37.                  // we copy now the arguments into the text gadget
  38.                // if we have supplied some...
  39.                  
  40.                SetGadgetValue( wh->olist, GADGET_ID_TEXT, (ULONG) args );
  41.                                                    
  42.                while( TRUE ) 
  43.                   {
  44.                         // I use here Wait(), so it is easier with the changes
  45.                  
  46.                       wh->signals = Wait( 1 << wh->win->UserPort->mp_SigBit );   // wait for window events
  47.                                                         
  48.                       if( wh->signals & 1 << wh->win->UserPort->mp_SigBit )
  49.                             if( HandleWindow(wh) )
  50.                                    break;        // does end the while loop                                       
  51.                      
  52.                   }
  53.              
  54.                CloseConfigWindow( wh->win ); 
  55.              }
  56.              
  57.           FreeMemH( wh ); // free our memory
  58.       }                            
  59. }
  60.  
  61. /********************************************************************/
  62. // This function does open our window. We could have done this in the
  63. // function OwnWindow() too, but we need this function later again.
  64.  
  65. BOOL OpenDOpusWin( WindowHandle *wh )
  66. {
  67.     NewConfigWindow ncfgwin;     // we need a NewConfigWindow structure too
  68.                                  // of couse you could also allocate it with
  69.                                           // AllocMemH()...
  70.     
  71.     // and have to fill it
  72.     
  73.     ncfgwin.nw_Parent = wh->screen;  // open on this screen
  74.     
  75.     // getting a localized title...
  76.     ncfgwin.nw_Title  = DOpusGetString( locale, MSG_WINDOW_TITLE );
  77.     
  78.     ncfgwin.nw_Dims   = &cfgwin; // a pointer to the ConfigWin structure
  79.     ncfgwin.nw_Locale = locale;  // the module locale pointer (from modinit.c)
  80.     ncfgwin.nw_Port   = NULL;    // we doesn't supply a port
  81.     ncfgwin.nw_Font   = NULL;    // just taking the screen font
  82.     ncfgwin.nw_Flags  = WINDOW_REQ_FILL      | // fill with stripple pattern
  83.                         WINDOW_AUTO_KEYS     | // handle keys automatic
  84.                               WINDOW_SCREEN_PARENT;  // nw_Parent points to a screen
  85.     
  86.     if( (wh->win = OpenConfigWindow(&ncfgwin)) )   // open the window
  87.       {
  88.           if( (wh->olist = AddObjectList(wh->win, odef)) ) // add the gadgets
  89.                   return TRUE;
  90.                     
  91.           CloseConfigWindow( wh->win );    //    in error case do not forget :-)
  92.       }
  93.     
  94.     return FALSE;
  95. }
  96.  
  97.  
  98. /********************************************************************/
  99. // we does only close the window, if the closegadget was pressed
  100. // if you want to close it within a gadget, you must only in the
  101. // right case set "stop" to TRUE
  102.  
  103. BOOL HandleWindow( WindowHandle *wh )
  104. {
  105.      BOOL stop = FALSE;
  106.      ULONG value;
  107.      
  108.      while( !stop && (wh->imsg = GetWindowMsg(wh->win->UserPort)) )
  109.         {
  110.             switch( wh->imsg->Class )  // let's handle the IDCMP
  111.               {
  112.                   case IDCMP_GADGETUP:
  113.                                           switch( GET_ID(wh->imsg) )
  114.                                          {
  115.                                                          case GADGET_ID_CYCLE:  // we copy simply the same text to the text gadget
  116.             
  117.                                                                      value = GetGadgetValue( wh->olist, GADGET_ID_CYCLE ) + MSG_CLICK_ME;
  118.                                                                             SetGadgetValue( wh->olist, GADGET_ID_TEXT, (ULONG) DOpusGetString(locale, value) );
  119.                                                                             break;
  120.             
  121.                                               case GADGET_ID_OKAY:   // doing a message
  122.                                                                      
  123.                                                                                         SetGadgetValue( wh->olist, GADGET_ID_TEXT, (ULONG) DOpusGetString(locale, MSG_OKAY_DONE) );
  124.                                                                             break;
  125.                                                      
  126.                                               case GADGET_ID_CANCEL: 
  127.                                                                      SetGadgetValue( wh->olist, GADGET_ID_TEXT, (ULONG) DOpusGetString(locale, MSG_CANCEL_DONE) );
  128.                                                                             break;
  129.                                           }
  130.                                                                  
  131.                                                   break;
  132.                                      
  133.                   case IDCMP_CLOSEWINDOW:
  134.                                           // we can not simply return here, the IntuiMessage must replied first
  135.                                                   
  136.                                                stop = TRUE;
  137.                                                   break;                                     
  138.               }
  139.                                  
  140.             ReplyWindowMsg( wh->imsg );  
  141.                               
  142.             // remember: You should not use any other routines
  143.             // to get/reply the messages of this window than
  144.             // GetWindowMsg() and ReplyWindowMsg() !!
  145.        }
  146.                                
  147.     return stop;
  148. }
  149.